package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.Role;
import com.lovo.bean.RoleDistribution;
import com.lovo.bean.Throne;
import com.lovo.service.RoleService;
import com.lovo.service.UserRoleService;
import com.lovo.service.serviceImpl.RoleServiceImpl;
import com.lovo.service.serviceImpl.UserRoleServiceImpl;
import com.lovo.util.CodeMsg;
import com.lovo.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RoleServlet",urlPatterns = "/role.do")
public class RoleServlet extends HttpServlet {
    private RoleService rs = new RoleServiceImpl();
    private UserRoleService urs = new UserRoleServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if (method == null || "".equalsIgnoreCase(method)) {
            String page = request.getParameter("page");
            int pageNo = 0;
            if (page == null || "".equalsIgnoreCase(page)) {
                pageNo = 1;
            } else {
                pageNo = Integer.parseInt(page);
            }
            String limit = request.getParameter("limit");
            PageInfo<Role> pageInfo = rs.getRoles(pageNo, Integer.parseInt(limit));
            Result<PageInfo<Role>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("addRole")){
            String roleName = request.getParameter("roleName");

            Role r = new Role();
            r.setRoleName(roleName);

            boolean flag = rs.addRole(r);

            Result<String> result = Result.success(String.valueOf(flag));
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(result);
            response.getWriter().write(s);
        }else if(method.equalsIgnoreCase("queryRole")){
            String page = request.getParameter("page");
            int pageNo = 0;
            if (page == null || "".equalsIgnoreCase(page)) {
                pageNo = 1;
            } else {
                pageNo = Integer.parseInt(page);
            }
            String limit = request.getParameter("limit");
            String roleName = request.getParameter("roleName");

            PageInfo<Role> pageInfo = rs.getRolesByQuery(pageNo, Integer.parseInt(limit),roleName);
            Result<PageInfo<Role>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("deleteRole")){
            String roleId = request.getParameter("roleId");
            boolean flag = rs.deleteRole(Integer.parseInt(roleId));

            Result<String> result = Result.success(String.valueOf(flag));
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(result);
            response.getWriter().write(s);

        }else if(method.equalsIgnoreCase("checkRole")){
            String roleId = request.getParameter("roleId");
            String page = request.getParameter("page");
            int pageNo = 0;
            if (page == null || "".equalsIgnoreCase(page)) {
                pageNo = 1;
            } else {
                pageNo = Integer.parseInt(page);
            }
            String limit = request.getParameter("limit");

            PageInfo<Throne> pageInfo = rs.getRoleThrone(pageNo, Integer.parseInt(limit),Integer.parseInt(roleId));
            Result<PageInfo<Throne>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("roleDistribution")){
            PageInfo<RoleDistribution> pageInfo = rs.getAllRoles();
            Result<PageInfo<RoleDistribution>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("getUserRole")){
            String userId = request.getParameter("userId");
            PageInfo<Integer> pageInfo = rs.getUserRole(Integer.parseInt(userId));
            Result<PageInfo<Integer>> result = null;
            if (pageInfo == null) {
                result = Result.error(CodeMsg.NOTFOUND_ERROR);
            } else {
                result = Result.success(pageInfo);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(result);
            response.getWriter().write(json);
        }else if(method.equalsIgnoreCase("changeUserRole")){
            String value = request.getParameter("value");
            String userId = request.getParameter("userId");

            boolean flag = urs.deleteByUser(Integer.parseInt(userId));
            boolean flag1 = urs.addRoleByUser(value,Integer.parseInt(userId));

            Result<String> result = Result.success(String.valueOf(flag1));
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(result);
            response.getWriter().write(s);

        }
    }
}
